package com.jzo2o.customer.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.api.customer.dto.response.AddressBookResDTO;
import com.jzo2o.api.publics.MapApi;
import com.jzo2o.api.publics.dto.response.LocationResDTO;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.common.utils.NumberUtils;
import com.jzo2o.common.utils.StringUtils;
import com.jzo2o.customer.mapper.AddressBookMapper;
import com.jzo2o.customer.model.domain.AddressBook;
import com.jzo2o.customer.model.dto.request.AddressBookPageQueryReqDTO;
import com.jzo2o.customer.model.dto.request.AddressBookUpsertReqDTO;
import com.jzo2o.customer.service.AddressBookService;
import com.jzo2o.mvc.utils.UserContext;
import com.jzo2o.mysql.utils.PageUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 地址薄 服务实现类
 * </p>
 *
 * @author itcast
 * @since 2023-07-06
 */
@Service
public class AddressBookServiceImpl extends ServiceImpl<AddressBookMapper, AddressBook> implements AddressBookService {

    @Resource
    AddressBookMapper addressBookMapper;
    @Resource
    MapApi mapApi;
    @Override
    public void add(AddressBookUpsertReqDTO addressBookUpsertReqDTO) {
        Long aLong = UserContext.currentUserId();
        //如果新增地址设为默认，取消其他默认地址
        if(1==addressBookUpsertReqDTO.getIsDefault()){
            cancelDefault(aLong);
        }

        AddressBook addressBook = BeanUtil.toBean(addressBookUpsertReqDTO, AddressBook.class);
        addressBook.setUserId(aLong);
        //组装地址
        String completeAddress = addressBookUpsertReqDTO.getProvince()
                + addressBookUpsertReqDTO.getCity()
                + addressBookUpsertReqDTO.getCounty()
                + addressBookUpsertReqDTO.getAddress();

        if(ObjectUtil.isEmpty(addressBookUpsertReqDTO.getLocation())){
            //获取高德经纬度
            LocationResDTO locationByAddress = mapApi.getLocationByAddress(completeAddress);
            //经纬度(字符串格式：经度,纬度),经度在前，纬度在后
            String location = locationByAddress.getLocation();
            addressBookUpsertReqDTO.setLocation(location);
        }
        if(StringUtils.isNotEmpty(addressBookUpsertReqDTO.getLocation())){
            addressBook.setLon(NumberUtils.parseDouble(addressBookUpsertReqDTO.getLocation().split(",")[0]));
            addressBook.setLat(NumberUtils.parseDouble(addressBookUpsertReqDTO.getLocation().split(",")[1]));
        }
        addressBookMapper.insert(addressBook);
    }

    private void cancelDefault(Long aLong) {
        LambdaUpdateWrapper<AddressBook> set = Wrappers.<AddressBook>lambdaUpdate()
                .eq(AddressBook::getUserId, aLong)
                .set(AddressBook::getIsDefault, 0);
        super.update(set);
    }

    @Override
    public PageResult<AddressBookResDTO> page(AddressBookPageQueryReqDTO addressBookPageQueryReqDTO) {
        Page<AddressBook> page = PageUtils.parsePageQuery(addressBookPageQueryReqDTO, AddressBook.class);
        LambdaQueryWrapper<AddressBook> queryWrapper = Wrappers.<AddressBook>lambdaQuery()
                .eq(AddressBook::getUserId, UserContext.currentUserId());
        Page<AddressBook> selectPage = addressBookMapper.selectPage(page, queryWrapper);
        return PageUtils.toPage(selectPage,AddressBookResDTO.class);
    }

    @Override
    public void update(long id, AddressBookUpsertReqDTO addressBookUpsertReqDTO) {
        if(1==addressBookUpsertReqDTO.getIsDefault()){
            cancelDefault(UserContext.currentUserId());
        }
        AddressBook addressBook = BeanUtil.toBean(addressBookUpsertReqDTO, AddressBook.class);
        addressBook.setId(id);
        String completeAddress = addressBookUpsertReqDTO.getProvince()
                + addressBookUpsertReqDTO.getCity()
                + addressBookUpsertReqDTO.getCounty()
                + addressBookUpsertReqDTO.getAddress();
        LocationResDTO locationByAddress = mapApi.getLocationByAddress(completeAddress);
        String location = locationByAddress.getLocation();
        if(StringUtils.isNotEmpty(location)){
            // 经度
            addressBook.setLon(NumberUtils.parseDouble(locationByAddress.getLocation().split(",")[0]));
            // 纬度
            addressBook.setLat(NumberUtils.parseDouble(locationByAddress.getLocation().split(",")[1]));
        }
        addressBookMapper.updateById(addressBook);
    }

    @Override
    public AddressBookResDTO defaultAddress() {
        LambdaQueryWrapper<AddressBook> queryWrapper = Wrappers.<AddressBook>lambdaQuery()
                .eq(AddressBook::getUserId,UserContext.currentUserId())
                .eq(AddressBook::getIsDefault,1);
        AddressBook addressBook = addressBookMapper.selectOne(queryWrapper);
       return BeanUtil.toBean(addressBook, AddressBookResDTO.class);

    }

    @Override
    public void updateDefaultStatus(Long aLong, long id, Integer flag) {
        if(1==flag){
            cancelDefault(UserContext.currentUserId());
        }
        AddressBook addressBook = new AddressBook();
        addressBook.setId(id);
        addressBook.setIsDefault(flag);
        addressBookMapper.updateById(addressBook);
    }

}
